package com.mango.community.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @Author:  MangoCookieMilk
 * @Date:     2020/8/15 22:53
 * redis 的配置类
 */
@Configuration
public class RedisConfig {

    /** redis 的配置
     * @param factory  RedisConnectionFactory  redis访问数据库的工厂
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        //给template设置访问数据库的工厂
        template.setConnectionFactory(factory);
        //设置redis的key的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        //设置redis普通的value的序列化方式
        template.setValueSerializer(RedisSerializer.json());
        //设置redis中hash的key的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        //设置redis中hash的value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());
        //是template中的设置生效
        template.afterPropertiesSet();
        return template;
    }
}
